package com.atguigu5.dao;

import com.beans.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @auther user
 * @date 2021/12/10 - 7:30
 */
public class UserDaoImpl implements UserDao {
    QueryRunner queryRunner = new QueryRunner();

    @Override
    public int inert(Connection connection, User user) {
        int count = 0;
        try {
            //QueryRunner queryRunner = new QueryRunner();
            String sql = "insert into user(name,password,address,phone) values(?,?,?,?)";
            count = queryRunner.update(connection, sql, user.getName(), user.getPassword(), user.getAddress(), user.getPhone());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int deleteById(Connection connection, int id) {
        int count = 0;
        try {
            //QueryRunner queryRunner = new QueryRunner();
            String sql = "delete from user where id = ?";
            count = queryRunner.update(connection, sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int updateById(Connection connection, User user) {
        int count = 0;
        try {
            //QueryRunner queryRunner = new QueryRunner();
            String sql = "update user set name = ?,password = ?,address = ?,phone =? where id = ?";
            count = queryRunner.update(connection, sql,user.getName(),user.getPassword(),user.getAddress(),user.getPhone(),user.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public User getUserById(Connection connection, int id) {
        User user = null;
        try {
            //QueryRunner queryRunner = new QueryRunner();
            String sql = "select id,name,password,address,phone from user where id = ?";
            BeanHandler<User> handler = new BeanHandler<>(User.class);
            user = queryRunner.query(connection, sql, handler, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public List<User> getAll(Connection connection) {
        List<User> userList = null;
        try {
            //QueryRunner queryRunner = new QueryRunner();
            String sql = "select id,name,password,address,phone from user";
            BeanListHandler<User> handler = new BeanListHandler<>(User.class);
            userList = queryRunner.query(connection, sql, handler);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public Long getCount(Connection connection) {
        Long count = null;
        try {
            //QueryRunner queryRunner = new QueryRunner();
            String sql = "select count(*) from user";
            ScalarHandler handler = new ScalarHandler();
            count = (Long) queryRunner.query(connection, sql, handler);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}
